<template>
  <div class="source-card-wrapper" @click="gotoPublish">
    <div class="source-main">
      <img :src="fileIcon" alt="pdf">
      <a class="hover"></a>
    </div>

    <div class="source-intro">
      <div class="pub-info">
        <span>{{source.author_name}}</span> <span style="color: var(--gray)">|</span> 
        {{formatPublishDate}}
      </div>
      <h3 class="source-title">
        {{source.title}}
      </h3>
      <h3 class="source-abstract">
        {{source.digest}}
      </h3>
    </div>
    
    <button class="delete-draft-button" @click.stop="deleteDraft">
      删除
    </button>
  </div>
</template>

<script>

  export default {
    props: {
      source: Object
    },
    computed: {
      fileIcon() {
        return this.$getFileIcon(this.source.source_name);
      },
      formatPublishDate() {
        return this.$dateUtils.format(this.source.publish_date, 'yyyy-MM-dd');
      }
    },
    methods: {
      gotoPublish() {
        this.$open(`/publish/source?draftId=${this.source.id}`)
      },
      deleteDraft() {
        this.$confirm("确定删除这个草稿吗?").then(flag => {
          if(flag) {
            this.$post({
              name: 'delete draft',
              url: 'source/content/draft/delete',
              data: {
                draft_id: this.source.id
              }
            }).then(res => {
              if(res.data.success) {
                this.$alert("删除成功")
              } else {
                this.$alert({
                  type: 'error',
                  content: '删除失败'
                });
              }
            }).catch(error => {
              this.$alert({
                type: 'error',
                content: '删除失败'
              })
            })
          }
        })
      }
    },
    mounted () {
    },
  }
</script>

<style lang="less" scoped>
.source-card-wrapper {
  --scw: var(--sourceCardWidth);
  --sch: var(--sourceCardHeight);
  position: relative;
  box-sizing: border-box;
  width: var(--scw);
  height: var(--sch);
  padding: calc(var(--sch) * 0.1) calc(var(--scw) * 0.04);

  &:hover {
    background-color: rgb(249, 249, 249);
    cursor: pointer;
  }

  .source-intro {
    float: left;
    width: 60%;
    text-align: left;
    pointer-events: none;

    .pub-info {
      font-size: calc(var(--sch) * 0.09);
      line-height: 2;
    }

    .source-title {
      font-size: calc(var(--sch) * 0.11);
      font-weight: bold;
      color: var(--dark);
      line-height: 2;
      margin-bottom: calc(var(--sch) * 0.05);
    }

    .source-abstract {
      font-size: calc(var(--sch) * 0.09);
      color: var(--dark);
      line-height: 1.5;
      display: -webkit-box;
      overflow: hidden;  
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

  .source-main {
    float: left;
    margin-right: 3%;
    height: 100%;
    padding: 0 5px;
    display: flex;
    justify-content: center;
    align-items: center;

    &:hover {
      cursor: pointer;

      .hover {
        display: block;
      }
    }

    img {
      height: 90%;
    }

    .hover {
      display: none;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 5px;
      background-color: var(--light);
    }
  }

  .delete-draft-button {
    position: absolute;
    top: 8%;
    right: 3%;
    font-size:  14px;
    padding: 5px 8px;
    transition: .3s;
    border: 1px solid var(--gray);
    border-radius: 3px;

    &:hover {
      border-color: var(--lightBlue);
      color: var(--darkBlue);
    }
  }
}
</style>